package fu.capstone.hanoicityguide.common.onlinemapultility;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

import com.mapquest.android.maps.AnnotationView;
import com.mapquest.android.maps.DefaultItemizedOverlay;
import com.mapquest.android.maps.GeoPoint;
import com.mapquest.android.maps.ItemizedOverlay;
import com.mapquest.android.maps.MapView;
import com.mapquest.android.maps.MyLocationOverlay;
import com.mapquest.android.maps.Overlay;
import com.mapquest.android.maps.OverlayItem;
import com.mapquest.android.maps.RouteManager;
import com.mapquest.android.maps.RouteManager.RouteCallback;
import com.mapquest.android.maps.RouteResponse;
import com.mapquest.android.maps.ServiceResponse.Info;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.GeoCoordinate;

public class OnlineMapUtil {

	private MyLocationOverlay mylocationoverlay;
	private AnnotationView annotation;

	public OnlineMapUtil() {

	}

	public AnnotationView getAnnotation() {
		return this.annotation;
	}

	public void setupmap(MapView map) {

		GeoPoint defaultHanoi = new GeoPoint(20.9, 106);
		// focus on the location
		map.getController().setCenter(defaultHanoi);
		// zoom level
		map.getController().setZoom(10);
		// set zoom control
		map.setBuiltInZoomControls(true);
		map.getController().zoomIn();
	}

	public void createRoute(final Context context, MapView map,
			WebView instruction, String endpoint) {
		// ------------api 2.x
		// LocationManager mgr =
		// (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		// android.location.Location lastKnownLoc =
		// mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		//
		// GeoPoint currentLocation = new GeoPoint(lastKnownLoc.getLatitude(),
		// lastKnownLoc.getLongitude());

		// ----------api 4.x
		// LocationClient mLocationClient = new LocationClient(context, null,
		// null);
		// mLocationClient.connect();
		// Location currentLocation;
		// currentLocation = mLocationClient.getLastLocation();

		try {
			// hopeless
			mylocationoverlay = new MyLocationOverlay(context, map);
			mylocationoverlay.enableCompass();
			mylocationoverlay.enableMyLocation();

			String provider;
			LocationManager locManager = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			Criteria criteria = new Criteria();
			provider = locManager.getBestProvider(criteria, false);
			GeoPoint currentLocation = null;
			Location lastKnownLoc = locManager
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			currentLocation = new GeoPoint(lastKnownLoc.getLatitude(),
					lastKnownLoc.getLongitude());

			String s_currentLocation = String.valueOf(currentLocation)
					.substring(9);

			RouteManager routeManager = new RouteManager(context, context
					.getResources().getString(R.string.api_key));
			routeManager.setMapView(map);
			// String startpoint = "21.0367, 105.8347";
			// String endpoint = "21.0206, 105.7638";
			routeManager.setItineraryView(instruction);
			routeManager.createRoute(s_currentLocation, endpoint);
			routeManager.setRouteCallback(new RouteCallback() {

				@Override
				public void onSuccess(RouteResponse arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onError(RouteResponse routeResponse) {
					// TODO Auto-generated method stub
					Info info = routeResponse.info;
					int statusCode = info.statusCode;

					StringBuilder message = new StringBuilder();
					message.append("Unable to create route.\n")
							.append("Error: ").append(statusCode).append("\n")
							.append("Message: ").append(info.messages);
					Toast.makeText(context, message.toString(),
							Toast.LENGTH_LONG).show();
				}
			});
		} catch (Exception e) {
		}
	}

	public void createRoute(final Context context, MapView map,
			WebView instruction, List<String> arr_itinerary_geo) {

		try {
			// hopeless
			mylocationoverlay = new MyLocationOverlay(context, map);
			mylocationoverlay.enableCompass();
			mylocationoverlay.enableMyLocation();

			String provider;
			LocationManager locManager = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			Criteria criteria = new Criteria();
			provider = locManager.getBestProvider(criteria, false);
			GeoPoint currentLocation = null;
			Location lastKnownLoc = locManager
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			currentLocation = new GeoPoint(lastKnownLoc.getLatitude(),
					lastKnownLoc.getLongitude());
			String s_currentLocation = String.valueOf(currentLocation)
					.substring(9);

			arr_itinerary_geo.add(0, s_currentLocation);
			RouteManager routeManager = new RouteManager(context, context
					.getResources().getString(R.string.api_key));
			routeManager.setMapView(map);
			// String startpoint = "21.0367, 105.8347";
			// String endpoint = "21.0206, 105.7638";
			routeManager.setItineraryView(instruction);
			routeManager.createRoute(arr_itinerary_geo);
			routeManager.setRouteCallback(new RouteCallback() {

				@Override
				public void onSuccess(RouteResponse arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onError(RouteResponse routeResponse) {
					// TODO Auto-generated method stub
					Info info = routeResponse.info;
					int statusCode = info.statusCode;

					StringBuilder message = new StringBuilder();
					message.append("Unable to create route.\n")
							.append("Error: ").append(statusCode).append("\n")
							.append("Message: ").append(info.messages);
					Toast.makeText(context, message.toString(),
							Toast.LENGTH_LONG).show();
				}
			});
		} catch (Exception e) {
		}
	}

	public void getcurrentLocation(Context context, MapView map) {

		mylocationoverlay = new MyLocationOverlay(context, map);
		mylocationoverlay.enableCompass();
		mylocationoverlay.enableMyLocation();

		GeoPoint currentlocation = mylocationoverlay.getMyLocation();
		map.getController().animateTo(currentlocation);
		map.getController().setZoom(11);
		// List<Overlay> test = map.getOverlays();
		map.getOverlays().add(mylocationoverlay);
		mylocationoverlay.setFollowing(true);

	}

	public void createMarker(Context context, MapView map, GeoPoint place,
			String placeName, String placeType) {

		// OverlayItem item1 = new OverlayItem(startpoint_marker, "A",
		// "Start point");
		OverlayItem item = new OverlayItem(place, placeName, placeType);

		Drawable icon = context.getResources().getDrawable(
				R.drawable.location_marker);
		final DefaultItemizedOverlay overlay = new DefaultItemizedOverlay(icon);
		// overlay.addItem(item1);
		overlay.addItem(item);

		map.getOverlays().add(overlay); // show added overlay to mapview
		map.invalidate();

		annotation = new AnnotationView(map);
		overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
			@Override
			public void onTap(GeoPoint pt, MapView mapView) {
				// when tapped, show the annotation for the overlayItem
				int lastTouchedIndex = overlay.getLastFocusedIndex();
				if (lastTouchedIndex > -1) {
					OverlayItem tapped = overlay.getItem(lastTouchedIndex);
					annotation.showAnnotationView(tapped);
				}
			}
		});
	}

	public void createMarker(Context context, MapView map,
			final DefaultItemizedOverlay overlay, GeoPoint place,
			String placeName, String placeType) {

		// OverlayItem item1 = new OverlayItem(startpoint_marker, "A",
		// "Start point");
		OverlayItem item = new OverlayItem(place, placeName, placeType);

		// Drawable icon =
		// context.getResources().getDrawable(R.drawable.location_marker);
		// overlay = new DefaultItemizedOverlay(icon);
		// overlay.addItem(item1);
		overlay.addItem(item);

		map.getOverlays().add(overlay); // show added overlay to mapview
		map.invalidate();

		annotation = new AnnotationView(map);
		overlay.setTapListener(new ItemizedOverlay.OverlayTapListener() {
			@Override
			public void onTap(GeoPoint pt, MapView mapView) {
				// when tapped, show the annotation for the overlayItem
				int lastTouchedIndex = overlay.getLastFocusedIndex();
				if (lastTouchedIndex > -1) {
					OverlayItem tapped = overlay.getItem(lastTouchedIndex);
					annotation.showAnnotationView(tapped);
				}
			}
		});
	}

	public GeoPoint convertGeocoordinatetoGeopoint(GeoCoordinate gc) {
		GeoPoint gp = new GeoPoint(gc.getLatitude(), gc.getLongitude());
		return gp;
	}

}
